<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/1999/XSL/Format"
	xmlns:fo="http://www.w3.org/1999/XSL/Format"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:v="livcos.org/ns/view"
  exclude-result-prefixes="fo xs f v">
	
	<xsl:import href="base.xsl"/>
	<xsl:import href="/livcos.org/common/default.xsl"/>
	
	<xsl:param name="ref"/>
	
	<xsl:variable name="logo" select="''"/>
	
	<xsl:output method="xml" media-type="application/pdf"/>

	<xsl:variable name="contentBaseUris" select="resolve-uri('ns', $taskDefId), resolve-uri('ns')"/>
	
	<xsl:template match="/">
		<xsl:variable name="prep">
			<xsl:variable name="_objId" select="f:find-data-object($ref)"/>
			<xsl:choose>
				<xsl:when test="$_objId != ''">
					<xsl:variable name="obj" select="doc($_objId)/*"/>
					<xsl:variable name="nsViews"
						select="f:find-objs-for-namespaces($obj, $contentBaseUris, 'view')"/>
					<xsl:sequence select="f:transform('', $obj, (), (resolve-uri('ns/default'), $nsViews))"/>
				</xsl:when>
				<xsl:otherwise>
					<root>
						<block margin-top="18pt" margin-bottom="16pt">
							No data object found for '<xsl:value-of select="$ref"/>'!
						</block>
					</root>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:apply-templates select="$prep/*" mode="root"/>
	</xsl:template>
	
	<xsl:template match="/fo:root" mode="root">
		<root>
			<xsl:copy-of select="@*"/>
			<layout-master-set>
				<xsl:call-template name="layout"/>
			</layout-master-set>
			<declarations>
				<x:xmpmeta xmlns:x="adobe:ns:meta/">
					<xsl:apply-templates select="v:head" mode="meta"/>
				</x:xmpmeta>
			</declarations>
			<xsl:call-template name="page-sequence"/>
		</root>
	</xsl:template>
	
	<xsl:template name="layout">
		<simple-page-master master-name="page_A4" page-height="29.7cm" page-width="21cm"
			margin-top="1.5cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
			<region-body margin-top="1.6cm" margin-bottom="1cm" margin-left="0.5cm"
				margin-right="0.5cm" />
			<region-before extent="1.6cm" />
			<region-after extent="1cm" />
		</simple-page-master>
	</xsl:template>
	
	<xsl:template name="page-sequence">
		<page-sequence master-reference="page_A4">
			<xsl:if test="v:head">
				<static-content flow-name="xsl-region-before">
					<xsl:apply-templates select="v:head" mode="header"/>
				</static-content>
				<static-content flow-name="xsl-region-after">
					<xsl:apply-templates select="v:head" mode="footer"/>
				</static-content>
			</xsl:if>
			<flow flow-name="xsl-region-body" font-size="8pt" font-family="Verdana">
				<block margin-top="18pt" margin-bottom="16pt" font-size="16pt">
					<xsl:value-of select="v:head/v:context"/>
				</block>
				<block margin-top="12pt" margin-bottom="64pt" font-size="24pt">
					<xsl:value-of select="v:head/v:title" />
				</block>
				<xsl:call-template name="toc"/>
				<block break-before="page"/>
				<xsl:apply-templates select="*"/>
				<block id="last_page"></block>
			</flow>
		</page-sequence>
	</xsl:template>
	
	<xsl:template match="v:head" mode="meta">
		<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
			<rdf:Description rdf:about=""	xmlns:dc="http://purl.org/dc/elements/1.1/">
				<!-- Dublin Core properties go here -->
				<dc:title><xsl:value-of select="v:title"/></dc:title>
				<dc:creator><xsl:value-of select="v:author"/></dc:creator>
				<dc:description><xsl:value-of select="v:context"/></dc:description>
			</rdf:Description>
			<rdf:Description rdf:about=""	xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
				<!-- XMP properties go here -->
				<xmp:ModifyDate><xsl:value-of select="v:last-modified"/>
					<xsl:variable name="l" select="normalize-space(v:last-modified)"/>
					<xsl:value-of select="if ($l != '') then $l else current-dateTime()"/>
				</xmp:ModifyDate>
				<pdf:Keywords><xsl:value-of select="v:keywords"/></pdf:Keywords>
			</rdf:Description>
		</rdf:RDF>
	</xsl:template>
	
	<xsl:template match="v:head" mode="header">
		<table table-layout="fixed" width="100%" border-bottom-style="solid" border-bottom-width="thin">
			<table-column column-width="25%"/>
			<table-column column-width="50%"/>
			<table-column column-width="25%"/>
			<table-body>
				<table-row>
					<table-cell padding="0pt">
						<block text-align="start">
							<xsl:if test="normalize-space($logo) != ''">
								<external-graphic src="{$logo}" content-height="25pt" vertical-align="middle"/>
							</xsl:if>
						</block>
					</table-cell>
					<table-cell padding="0pt">
						<block font-size="10pt" font-family="sans-serif" text-align="center" margin-top="15pt">
							<xsl:value-of select="v:context"/>
							<xsl:text>: </xsl:text>
							<xsl:value-of select="v:title"/>
						</block>
					</table-cell>
					<table-cell padding="0pt">
						<block font-size="10pt" font-family="sans-serif" text-align="end" margin-top="15pt">
							<page-number />/<page-number-citation ref-id="last_page"/>
						</block>
					</table-cell>
				</table-row>
			</table-body>
		</table>
	</xsl:template>
	
	<xsl:template match="v:head" mode="footer">
		<table table-layout="fixed" width="100%" border-top-style="solid" border-top-width="thin">
			<table-column column-width="20%"/>
			<table-column column-width="60%"/>
			<table-column column-width="20%"/>
			<table-body >
				<table-row>
					<table-cell padding="1pt">
						<block font-size="8pt" text-align="start">
							<xsl:value-of select="v:author"/>
						</block>
					</table-cell>
					<table-cell padding="1pt">
						<block font-size="8pt" text-align="center">
							<xsl:value-of select="f:object-id(..)" />
							<xsl:text>.pdf</xsl:text>
						</block>
					</table-cell>
					<table-cell padding="1pt">
						<block font-size="8pt" text-align="end">
							<xsl:variable name="l" select="normalize-space(v:last-modified)"/>
							<xsl:variable name="date" as="xs:dateTime" select="if ($l != '') then xs:dateTime($l) else current-dateTime()"/>
							<xsl:value-of select="format-date(xs:date($date), '[Y]-[M01]-[D01]')"/>
						</block>
					</table-cell>
				</table-row>
			</table-body>
		</table>
	</xsl:template>
	
	<xsl:template name="toc">
		<xsl:variable name="list">
			<xsl:apply-templates select="*" mode="toc"/>
		</xsl:variable>
		<xsl:if test="$list/*">
			<block break-before="page" xsl:use-attribute-sets="heading" font-size="12pt" margin-bottom="6pt">
				<xsl:text>Table of Contents</xsl:text>
			</block>
			<xsl:sequence select="$list/*"/>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="v:chapter[@id]" mode="toc">
		<block text-align-last="justify" space-before="1pt" space-after="1pt" font-size="8pt"
			margin-left="6mm" margin-right="12mm">
			<basic-link internal-destination="{@id}">
				<xsl:number level="multiple" count="v:chapter" format="1.1"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="v:title"/>
				<xsl:text> </xsl:text>
				<leader leader-length.minimum="12pt" leader-length.optimum="40pt"
					leader-length.maximum="100%" leader-pattern="dots"/>
				<xsl:text> </xsl:text>
				<page-number-citation ref-id="{@id}"/> 
			</basic-link>
		</block>
		<xsl:apply-templates select="*" mode="toc"/>
	</xsl:template>
	
	<xsl:template match="v:*" mode="toc">
	</xsl:template>
	
	<xsl:template match="v:chapter">
		<xsl:variable name="level" select="count(ancestor-or-self::v:chapter)"/>
		<block xsl:use-attribute-sets="heading" id="{@id}">
			<xsl:call-template name="set-heading-attributes">
				<xsl:with-param name="level" select="$level"/>
			</xsl:call-template>
			<xsl:number level="multiple" count="v:chapter" format="1.1"/>
			<xsl:text> </xsl:text>
			<xsl:sequence select="v:title/node()"/>
		</block>
		<xsl:apply-templates select="*"/>
	</xsl:template>
	
	<xsl:template match="v:*">
	</xsl:template>
	
</xsl:stylesheet>
